<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class AdminFilePanelController extends MiniyunController
{
    public $layout='//layouts/admin';
    public $mAction;
    
    public function actionIndex()
    {
        $this->mAction   = 'index';
        $this->_actionGo();
    }
    
    public function actionOffice()
    {
        $this->mAction   = 'office&office=1';
        $this->_actionGo();
    }
    
    public function actionImage()
    {
        $this->mAction   = 'image&image=1';
        $this->_actionGo();
    }
    
    public function actionTrash()
    {
        $this->mAction   = 'trash&trash=1';
        $this->_actionGo();
    }
    
    private function _actionGo()
    {
        $model= new FileListForm();
        if(Yii::app()->request->isPostRequest)
        {
            $model->attributes=$_POST['FileListForm'];
            if($model->formAction())
            {
                                return;
            }
        }else if(isset($_GET['FileListForm']))
        {
            $model->attributes=$_GET['FileListForm'];        }
        $userFile     = new UserFile;
        $this->render('fileList',array(
                        'model'        => $model,
                        'count'        => $userFile->count(),                        'officeCount'  => $userFile->officeCount(),                        'imageCount'   => $userFile->imageCount(),                        'deletedCount' => $userFile->deletedCount(),        ));
    }
    
    public function actionDownload()
    {
        $id              = Yii::app()->request->getParam('id');
        $versionID       = Yii::app()->request->getParam('version');
        $fileName        = Yii::app()->request->getParam('name');
        $this->mAction   = Yii::app()->request->getParam('action');
        $message = "获取文件信息失败, 下载取消";
        if (isset($id) && isset($versionID))
        {
            $model = FileVersion::model()->findByPk($versionID);
            if (!empty($model))
            {
                if ($this->_startDownload($model, $fileName))
                {
                    return;
                }
            }
        }
        if ($this->mAction === 'show')
        {
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, parent::getParamers(array('id'=>$id, 'message'=>$message))));
        } else
        {
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, parent::getParamers(array('message'=>$message))));
        }
    }

    
    public function actionRevert()
    {
        $model = new FileDeleteForm();
        $model->data = $model->search()->getData();
        $this->mAction  = Yii::app()->request->getParam('action');
        if(Yii::app()->request->isPostRequest){
            $count = $model->revertData();            $message = "还原  ".$count." 个文件成功";
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, array('message'=>$message)));
        }else{
            $this->render('fileRevert',array(
                     "data"=>$model->data,
            ));
        }
    }

    
    public function actionRemove()
    {
        $model = new FileDeleteForm();
        $model->data = $model->search()->getData();
        $this->mAction  = Yii::app()->request->getParam('action');
        if(Yii::app()->request->isPostRequest){
            $count = $model->removeData();            $message = $count." 个文件已放入回收站";
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, array('message'=>$message)));
        }else{
            $this->render('fileRemove',array(
                     "data"=>$model->data,
            ));
        }
    }

    
    public function actionDelete()
    {
        $model = new FileDeleteForm();
        $model->data = $model->search()->getData();
        $this->mAction  = Yii::app()->request->getParam('action');
        if(Yii::app()->request->isPostRequest){
            $count = $model->deleteData();            $message = "删除  ".$count." 个文件成功";
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, array('message'=>$message)));
        }else{
            $this->render('fileDelete',array(
                     "data"=>$model->data,
            ));
        }
    }

    
    public function actionDeleteAll()
    {
                        $message = '彻底删除全部文件';
        $this->redirect(Yii::app()->createUrl("adminFilePanel/index&message=".$message));
    }

    
    public function actionRemoveAll()
    {
        UserFile::model()->removeFileAll();
        $message = '删除全部文件';
        $this->redirect(Yii::app()->createUrl("adminFilePanel/index&message=".$message));
    }

    
    public function actionRevertAll()
    {
        UserFile::model()->revertFileAll();
        $message = '还原全部文件';
        $this->redirect(Yii::app()->createUrl("adminFilePanel/index&message=".$message));
    }

    
    public function actionShow()
    {
        $this->mAction  = Yii::app()->request->getParam('action');

        if(empty($_REQUEST['id'])){
            $message = "没有文件信息";
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction,array('message'=>$message)));
        }else{
            $id=$_REQUEST['id'];
        }

        $model = new FileBase();
        $model->load($id);
        $detailModel = new FileDetailForm();
        $detailModel->fileModel = $model;
        $this->mAction   = 'show';
        if (empty($model))
        {
            $message = "没有文件信息";
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction,array('message'=>$message)));

        }

        if(Yii::app()->request->isPostRequest)
        {
            $detailModel->attributes=$_POST['FileDetailForm'];

            if($detailModel->updateFormAction())
            {
                                return;
            }
        } else if(isset($_GET['FileDetailForm']))
        {
            $model->attributes=$_GET['FileListForm'];        }
        $this->render('show',array(
                                        'model' => $detailModel,));
    }
    
    public function actionRename()
    {
        $id            = Yii::app()->request->getParam('id');
        $name          = Yii::app()->request->getParam('name');
        $this->mAction = Yii::app()->request->getParam('action');
        $message = '文件更名失败';
        if (isset($id) && isset($name))
        {
            UserFile::model()->updateFileName($id, $name, $message);
        }

        if ($this->mAction === 'show')
        {
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, parent::getParamers(array('id'=>$id, 'message'=>$message))));
        } else
        {
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, parent::getParamers(array('message'=>$message))));
        }
    }

    
    public function actionVersion()
    {
        $id              = Yii::app()->request->getParam('id');
        $versionID       = Yii::app()->request->getParam('version');
        $this->mAction   = Yii::app()->request->getParam('action');
        if (isset($id) && isset($versionID))
        {
            UserFile::model()->revertVersion($id, $versionID);
        }
        $message = '文件版本恢复成功';
        if ($this->mAction === 'show')
        {
            $this->redirect(Yii::app()->createUrl("adminFilePanel/".$this->mAction, parent::getParamers(array('id'=>$id, 'message'=>$message))));
        }
    }

    
    public function actionStat(){
        $statForm = new FileStatForm();
        $statForm->model = new UserFile();
        if(Yii::app()->request->isPostRequest){
            $statForm->startDate = $_POST['FileStatForm']["startDate"];
            $statForm->endDate   = $_POST['FileStatForm']["endDate"];
            $statForm->initData();
        }else{
            $statForm->initData();        }
        $this->render('stat',array(
                 "data"=>$statForm,
        ));
    }

    
    private function _startDownload($model, $fileName)
    {
        $signature = $model["file_signature"];
        $contentType = $model["mime_type"];
        $contentType = empty ( $content_type ) ? "application/force-download" : $contentType;

        $parts = str_split(substr($signature,0,8), 2);
        $path = join("/", $parts);
        $path = $path . "/" . $signature;

       
        $filePath = Yii::app()->params['app']['path']."/upload_block/".$path;

        if (file_exists($filePath))
        {
            CUtils::downloadRange($filePath, $fileName, $contentType);
            return true;
        } else
        {
            return false;
        }
    }

}